package com.base.pig.controller;



import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.pig.dto.*;
import com.base.pig.entity.*;
import com.base.pig.service.*;
import com.base.pig.util.R;
import com.base.pig.vo.DeviceListVo;
import com.base.pig.vo.EventListVo;
import com.base.pig.vo.PendingMattersListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * <p>
 *  设备查询controller
 * </p>
 *
 * @author wangzhx
 * @since 2022-03-25
 */
@RestController
@Api(value = "DeviceSearchController", tags = "设备查询定位")
@RequestMapping("/DeviceSearchController")
public class DeviceSearchController {
    @Autowired
    public CommunityRenovationService communityRenovationService;

    @Autowired
    public PumpGateStationService pumpGateStationService;

    @Autowired
    public WaterQualityDeviceService waterQualityDeviceService;

    @Autowired
    public OptSiteService optSiteService;

    @Autowired
    public DeviceConfigService deviceConfigService;


    @ApiOperation(value = "设备配置查询")
    @PostMapping("/deviceConfigList")
    public R list(){
        JSONObject obj = new JSONObject();
        List<DeviceConfig> szList = deviceConfigService.list(Wrappers.<DeviceConfig>query().lambda().eq(DeviceConfig::getType,"水质柜").orderByAsc(DeviceConfig::getPosition));
        obj.put("水质柜",szList);
        List<DeviceConfig> ywList = deviceConfigService.list(Wrappers.<DeviceConfig>query().lambda().eq(DeviceConfig::getType,"压力水位计").orderByAsc(DeviceConfig::getPosition));
        obj.put("压力水位计",ywList);
        List<DeviceConfig> llList = deviceConfigService.list(Wrappers.<DeviceConfig>query().lambda().eq(DeviceConfig::getType,"管道流量监测仪").orderByAsc(DeviceConfig::getPosition));
        obj.put("管道流量监测仪",llList);
        return R.ok(obj,"查询成功");
    }

    @ApiOperation(value = "根据id修改设备配置")
    @PostMapping("/deviceConfigUpdate/{id}")
    public R update(@RequestBody DeviceConfig deviceConfig){
        if(deviceConfig.getId()!=null)
        {
            long count = deviceConfigService.count(Wrappers.<DeviceConfig>query().lambda().eq(DeviceConfig::getId,deviceConfig.getId()));
            if(count != 0) {
                return R.ok( deviceConfigService.updateById(deviceConfig),"修改成功");
            }
            return R.failed(null,"未找到该数据");
        }
        return R.failed(null,"id不能为空");
    }

    @ApiOperation(value = "查询")
    @PostMapping("/searchByName")
    public R searchByName(@RequestBody SearchDto dto){
        String type = dto.getType();
        String name = dto.getName();
        List<DeviceListVo> voList = new ArrayList<>();
        if(type.equals("水质柜")){
            List<WaterQualityDevice> list = waterQualityDeviceService.list(Wrappers.<WaterQualityDevice>query().lambda().like(WaterQualityDevice::getName,name));
            list.forEach(node ->{
                DeviceListVo vo = new DeviceListVo();
                vo.setDeviceId(node.getNum());
                vo.setDeviceTypeName(node.getMark());
                vo.setPointName(node.getName());
                vo.setSmX(node.getXzb());
                vo.setSmY(node.getYzb());
                voList.add(vo);
            });
        }else if(type.equals("改造小区")){
            List<CommunityRenovation> list = communityRenovationService.list(Wrappers.<CommunityRenovation>query().lambda().like(CommunityRenovation::getName,name));
            list.forEach(node ->{
                DeviceListVo vo = new DeviceListVo();
                vo.setDeviceId(node.getNum());
                vo.setDeviceTypeName(node.getMark());
                vo.setPointName(node.getName());
                vo.setSmX(node.getXzb());
                vo.setSmY(node.getYzb());
                voList.add(vo);
            });
        }else if(type.equals("泵站闸口")){
            List<PumpGateStation> list = pumpGateStationService.list(Wrappers.<PumpGateStation>query().lambda().like(PumpGateStation::getName,name));
            list.forEach(node ->{
                DeviceListVo vo = new DeviceListVo();
                vo.setDeviceId(node.getNum());
                vo.setDeviceTypeName(node.getMark());
                vo.setPointName(node.getName());
                vo.setSmX(node.getXzb());
                vo.setSmY(node.getYzb());
                voList.add(vo);
            });
        }else if(type.equals("监测点")){
            List<OptSite> list = optSiteService.list(Wrappers.<OptSite>query().lambda().like(OptSite::getName,name));
            list.forEach(node ->{
                DeviceListVo vo = new DeviceListVo();
                vo.setDeviceId(node.getNum());
                vo.setDeviceTypeName(node.getMark());
                vo.setPointName(node.getName());
                vo.setSmX(node.getXzb());
                vo.setSmY(node.getYzb());
                voList.add(vo);
            });
        }
        return R.ok(voList,"查询成功");
    }

    @ApiOperation(value = "画框个数统计")
    @PostMapping("/lengthStatistics")
    public R lengthStatistics(@RequestBody ScopeDto scopeDto){
        JSONObject obj = new JSONObject();
        Long szsum = waterQualityDeviceService.count(Wrappers.<WaterQualityDevice>query().lambda()
                .le(WaterQualityDevice::getXzb,scopeDto.getXmax())
                .le(WaterQualityDevice::getYzb,scopeDto.getYmax())
                .ge(WaterQualityDevice::getYzb,scopeDto.getYmin())
                .ge(WaterQualityDevice::getXzb,scopeDto.getXmin()));
        obj.put("水质柜",szsum);
        Long crsum = communityRenovationService.count(Wrappers.<CommunityRenovation>query().lambda()
                .le(CommunityRenovation::getXzb,scopeDto.getXmax())
                .le(CommunityRenovation::getYzb,scopeDto.getYmax())
                .ge(CommunityRenovation::getYzb,scopeDto.getYmin())
                .ge(CommunityRenovation::getXzb,scopeDto.getXmin()));
        obj.put("改造小区",crsum);
        Long pssum = pumpGateStationService.count(Wrappers.<PumpGateStation>query().lambda()
                .le(PumpGateStation::getXzb,scopeDto.getXmax())
                .le(PumpGateStation::getYzb,scopeDto.getYmax())
                .ge(PumpGateStation::getYzb,scopeDto.getYmin())
                .ge(PumpGateStation::getXzb,scopeDto.getXmin()));
        obj.put("泵站闸口",pssum);
        Long ossum = optSiteService.count(Wrappers.<OptSite>query().lambda()
                .le(OptSite::getXzb,scopeDto.getXmax())
                .le(OptSite::getYzb,scopeDto.getYmax())
                .ge(OptSite::getYzb,scopeDto.getYmin())
                .ge(OptSite::getXzb,scopeDto.getXmin()));
        obj.put("监测点",ossum);
        return R.ok(obj,"查询成功");
    }



}
